package com.syl.practice.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {
    public static void main(String[] args) {
        demo01();
        demo02();
    }

    // stream流式操作
    public static void demo01() {
        ArrayList<String> list = new ArrayList();
        list.add("谢尔顿");
        list.add("比利");
        list.add("乔治");
        list.add("汤米");
        list.add("玛丽");
        list.add("玛丽");
        Stream<String> stream = list.stream();

        //stream.limit(6).skip(2).distinct().filter(s -> !s.equals("比利")).forEach((System.out::println));
        List<String> stringList = stream.limit(6).skip(2).distinct().filter(s -> !s.equals("比利")).collect(Collectors.toList());
        stringList.remove(1);
        System.out.println(stringList);
    }

    // lambda表达式
    public static void demo02() {
        // 平常表达
        new Runnable() {
            @Override
            public void run() {
                System.out.println("hehe");
            }
        }.run();

        // lambda表达式
        Runnable runnable = () -> System.out.println("haha");
        runnable.run();

        new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("...");
            }
        }.accept("hello world!");

        Consumer<String> consumer = (s) -> System.out.println(s);
        consumer.accept("小提琴");

        Comparator<Integer> comparator1 = (o1, o2) -> {
            return o1.compareTo(o2);
        };
        System.out.println(comparator1.compare(1, 3));
    }
}
